<template>
  <div class="app-container">

    <u-page title="发票管理">

      <u-filtered>
        <el-form ref="listQuery" :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="listQuery.orderNo" placeholder="请输入订单号" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="业务订单号" prop="businessOrderNo">
            <el-input v-model="listQuery.businessOrderNo" placeholder="业务订单号" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="发票号码" prop="tradeNo">
            <el-input v-model="listQuery.tradeNo" placeholder="发票号码" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="listQuery.email" placeholder="邮箱" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="发票类型" prop="invoiceType">
            <el-select v-model="listQuery.invoiceType" placeholder="发票类型" class="status" clearable>
              <el-option label="全部" value="" />
              <el-option label="数电普通发票" :value="32" />
              <el-option label="数电专用发票" :value="31" />
              <el-option label="增值税电子普通发票" :value="1" />
              <el-option label="增值税专用发票" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="发票来源" prop="businessType">
            <el-select v-model="listQuery.businessType" placeholder="发票来源" class="status" clearable>
              <el-option label="全部" value="" />
              <el-option label="发票助手" :value="1" />
              <el-option label="厂商网" :value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="发票状态" prop="status">
            <el-select v-model="listQuery.status" placeholder="开票状态" class="status" clearable>
              <el-option label="全部" value="" />
              <el-option label="待开票" :value="0" />
              <el-option label="开票中" :value="1" />
              <el-option label="开票失败" :value="2" />
              <el-option label="已完成" :value="3" />
              <el-option label="已作废" :value="4" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间">
            <u-date-time-picker v-model="searchDate" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button v-if="row.status >= 3 && row.pdfUrl" type="text" @click="handleDownload(row.id)">下载</el-button>
              <el-button v-if="row.status === 1" type="text" @click="hdlSynchro(row)">同步</el-button>
              <el-button v-if="row.status === 2" type="text" @click="hdlRetry(row)">重开</el-button>
              <el-button type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button v-if="row.status === 3 && row.type === 0" type="text" @click="hdlCancel(row)">作废</el-button>
              <el-button type="text" @click="hdlDelete(row.id, row.orderNo)">删除</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" :footer="false" @confirm="hdlUpdate()">
      <el-form ref="dialogForm" :rules="rules" :model="dataForm" label-position="top" label-width="80px">

        <el-descriptions :column="2" border style="margin-bottom: 10px">
          <el-descriptions-item label="订单号">{{ dataForm.orderNo }}</el-descriptions-item>

          <el-descriptions-item label="业务订单号">{{ dataForm.businessOrderNo }}</el-descriptions-item>

          <el-descriptions-item label="发票号码">{{ dataForm.tradeNo }}</el-descriptions-item>

          <el-descriptions-item label="发票类型">
            <el-tag v-if="dataForm.invoiceType === 32" type="success">数电普通发票</el-tag>
            <el-tag v-if="dataForm.invoiceType === 31" type="success">数电专用发票</el-tag>
            <el-tag v-if="dataForm.invoiceType === 1" type="success">增值税电子普通发票</el-tag>
            <el-tag v-if="dataForm.invoiceType === 4" type="success">增值税专用发票</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="发票来源">
            <el-tag v-if="dataForm.businessType === 1" type="success">发票助手</el-tag>
            <el-tag v-if="dataForm.businessType === 2" type="success">厂商网</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="邮箱">
            <el-tag type="success">{{ dataForm.email }}</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="发票状态" :span="2">
            <el-tag v-if="dataForm.status === -1" type="info">已关闭</el-tag>
            <el-tag v-if="dataForm.status === 0" type="warning">待开票</el-tag>
            <el-tag v-if="dataForm.status === 1" type="warning">开票中</el-tag>
            <el-tag v-if="dataForm.status === 2" type="danger">开票失败</el-tag>
            <el-tag v-if="dataForm.status === 3" type="success">已完成</el-tag>
            <el-tag v-if="dataForm.status === 4" type="info">已作废</el-tag>
          </el-descriptions-item>

          <el-descriptions-item label="备注" :span="2">{{ dataForm.message }}</el-descriptions-item>

        </el-descriptions>

      </el-form>
    </u-dialog>

    <u-dialog title="请求内容" :show.sync="invoiceDataDialogVisible" :footer="false" width="45%" class="code">
      <el-input v-model="invoiceData" autosize type="textarea" disabled placeholder="{}" />
    </u-dialog>

    <u-dialog title="响应内容" :show.sync="dataDialogVisible" :footer="false" width="45%" class="code">
      <el-input v-model="data" autosize type="textarea" disabled placeholder="{}" />
    </u-dialog>

  </div>
</template>
<script>
export default {
  data() {
    return {
      api: this.$u.api.PluginsInvoice,
      // 表格
      columns: [
        {
          prop: 'type',
          label: '开票类型',
          width: '100',
          render: (h, params) => {
            const { type } = params.row
            let title = '蓝票'
            let theme = 'success'
            if (type === 1) {
              title = '红票'
              theme = 'danger'
            }
            return h('el-tag', { props: { type: theme }}, title)
          }
        },
        {
          prop: 'orderNo',
          label: '订单号',
          width: '200',
          render: (h, params) => {
            return h('el-tag', params.row.orderNo)
          }
        },
        {
          prop: 'tradeNo',
          label: '发票号码',
          width: '200',
          render: (h, params) => {
            if (params.row.tradeNo) {
              return h('el-tag', params.row.tradeNo)
            }
          }
        },
        {
          prop: 'businessOrderNo',
          label: '业务订单号',
          width: '200',
          render: (h, params) => {
            return h('el-tag', params.row.businessOrderNo)
          }
        },
        {
          prop: 'invoiceType',
          label: '发票类型',
          width: '120',
          render: (h, params) => {
            const { invoiceType } = params.row
            let title = ''
            if (invoiceType === 32) {
              title = '数电普通发票'
            } else if (invoiceType === 31) {
              title = '数电专用发票'
            } else if (invoiceType === 1) {
              title = '增值税电子普通发票'
            } else if (invoiceType === 4) {
              title = '增值税专用发票'
            }

            return h('el-tag', { props: { type: 'success' }}, title)
          }
        },
        {
          prop: 'businessType',
          label: '发票来源',
          width: '100',
          render: (h, params) => {
            const { businessType } = params.row
            let title = '发票助手'
            if (businessType === 1) {
              title = '发票助手'
            } else if (businessType === 2) {
              title = '厂商网'
            }
            return h('el-tag', { props: { type: 'success' }}, title)
          }
        },
        {
          prop: 'email',
          label: '邮箱'
        },
        {
          prop: 'invoiceData',
          label: '请求内容',
          render: (h, params) => {
            return h('el-button', { props: { type: 'text' }, on: {
              click: () => this.hdlInvoiceData(params.row)
            }}, '请求内容')
          }
        },
        {
          prop: 'data',
          label: '响应内容',
          render: (h, params) => {
            return h('el-button', { props: { type: 'text' }, on: {
              click: () => this.hdlData(params.row)
            }}, '响应内容')
          }
        },
        {
          prop: 'status',
          label: '发票状态',
          width: '100',
          render: (h, params) => {
            const { status } = params.row
            let title = '待开票'
            let type = 'warning'
            if (status === 1) {
              title = '开票中'
              type = 'warning'
            } else if (status === 2) {
              title = '开票失败'
              type = 'danger'
            } else if (status === 3) {
              title = '已完成'
              type = 'success'
            } else if (status === 4) {
              title = '已作废'
              type = 'info'
            }
            return h('el-tag', { props: { type }}, title)
          }
        },
        {
          prop: 'message',
          label: '备注'
        },
        {
          prop: 'createdAt',
          label: '创建时间',
          timestamp: true
        }
      ],

      list: [],
      listLoading: true,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        title: '',
        status: ''
      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑发票',
        create: '新增发票'
      },
      dataForm: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      newForm: {
        status: true
      },
      invoiceDataDialogVisible: false,
      invoiceData: {},
      dataDialogVisible: false,
      data: {}
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    // 重开发票
    hdlRetry(row) {
      const self = this
      // 模态对话框组件
      self.$confirm('订单号：' + row.orderNo + '，是否继续重开？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        // 处理确认事件
        self.api.retry({ orderNo: row.orderNo }).then(() => {
          // 刷新数据列表
          self.hdlList()
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    },
    // 作废发票
    hdlCancel(row) {
      const self = this
      // 模态对话框组件
      self.$confirm('订单号：' + row.orderNo + '，是否继续作废？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        // 处理确认事件
        self.api.cancel({ orderNo: row.orderNo }).then(() => {
          // 刷新数据列表
          self.hdlList()
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    },
    // 同步发票
    hdlSynchro(row) {
      const self = this
      // 模态对话框组件
      self.$confirm('订单号：' + row.orderNo + '，是否继续同步？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        // 处理确认事件
        self.api.synchro({ orderNo: row.orderNo }).then(() => {
          // 刷新数据列表
          self.hdlList()
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    },
    // 发票内容
    hdlInvoiceData(row) {
      const { invoiceData } = row
      const content = invoiceData ? JSON.parse(invoiceData) : {}
      this.invoiceData = JSON.stringify(content, null, '\t')
      this.invoiceDataDialogVisible = true
    },
    // 响应内容
    hdlData(row) {
      const { data } = row
      const content = data ? JSON.parse(data) : {}
      this.data = JSON.stringify(content, null, '\t')
      this.dataDialogVisible = true
    },
    // 文件下载事件
    handleDownload(id) {
      this.api.download(id)
    }
  }
}
</script>
<style lang="scss" scoped>

.app-container{
  ::v-deep {
    .filter {
      border: 0;
    }

    .code .el-textarea.is-disabled .el-textarea__inner {
      background-color: #060606 !important;
      color: #c6c6c6 !important;
    }

    .ack .el-input{
      width: 120px;
    }
  }
}
</style>
